//
//  XSTabBarViewController.m
//  MeiLiYiChu
//
//  Created by mac on 16/2/29.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "XSTabBarViewController.h"
#import "Masonry.h"
#import "XSMianViewController.h"
#import "XSBrandViewController.h"
#import "XSCatViewController.h"
#import "XSMYViewController.h"

@interface XSTabBarViewController ()
{
    NSArray * norArr;
    NSArray * selArr;
    NSInteger  current;
    
    
}
@end

@implementation XSTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    current =-1;
    _barViewControllers = [[NSMutableDictionary alloc]init];
    norArr=@[@"yuike_maintab_home_nor",@"yuike_maintab_category_nor",@"yuike_maintab_shopcart_nor",@"yuike_maintab_space_nor"];
    selArr=@[@"yuike_maintab_home_sel",@"yuike_maintab_category_sel",@"yuike_maintab_shopcart_sel",@"yuike_maintab_space_sel"];
    //移除自带的tabBar
    [self.tabBar removeFromSuperview];
    
    [self createTabBar];
    
    //默认显示第0个
    [self selectBtn:0];
}

-(void)createTabBar
{

    _tabBarView =[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"alertViewCustom_left"]];
    _tabBarView.userInteractionEnabled =YES;
    [self.view addSubview:_tabBarView];
    
    [_tabBarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(0);
        make.right.offset(0);
        make.bottom.offset(0);
        make.height.equalTo(@50);     
    }];
    

   
    
    UIButton * lastButton;
    
    for (int i = 0; i< 4; i++)
    {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag =i+1;
//        button.backgroundColor =[UIColor redColor];
        [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_tabBarView addSubview:button];

        UIImageView * imageView =[[UIImageView alloc]init];

        imageView.tag = i+20;
        if (i ==0)
        {
            imageView.image =[UIImage imageNamed:selArr[i]];
        }
        else
        {
            imageView.image =[UIImage imageNamed:norArr[i]];
        }
        [button addSubview:imageView];
        
        
        
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
       
            make.top.offset(0);
        
            make.bottom.offset(0);
        
        
            if (!lastButton) {
            
                make.left.offset(0);
        
            }
            else
            {
                make.left.equalTo(lastButton.mas_right).with.offset(0);
            
                make.width.equalTo(lastButton.mas_width);
            }
            if (i==3) {
                make.right.offset(0);
            }
        }];
        
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
           
            make.top.offset(0);
            make.bottom.offset(0);
            make.width.equalTo(@30);
            
            make.centerX.equalTo(button.mas_centerX);
        }];

        lastButton =button;
        
        
    }
    
}
-(void)btnClick:(UIButton *)btn
{
    NSInteger  index =btn.tag -1;
    //切换界面
    [self selectBtn:index];
  
}
-(void)selectBtn:(NSInteger)seletIndex
{

    if ( seletIndex ==current) {
        return;
    }
    UIViewController * viewController =nil;
    //不为0的时候切换图片
    if (current != -1)
    {
        [self highlitImageView:seletIndex];
        viewController  =  [_barViewControllers objectForKey:@(seletIndex)];
    }

    //若不存在
    if (!viewController)
    {
        switch (seletIndex) {
            case 0:
            {
                XSMianViewController * mainVC =[[XSMianViewController alloc]init];
                viewController =[[UINavigationController alloc]initWithRootViewController:mainVC];
                mainVC.tabBar =_tabBarView;
            }
                break;
            case 1:
            {
                
                XSBrandViewController * brandVC =[[XSBrandViewController alloc]init];
                viewController =[[UINavigationController alloc]initWithRootViewController:brandVC];
                
            }
                break;
            case 2:
            {
                XSCatViewController * catVC =[[XSCatViewController alloc]init];
                viewController =[[UINavigationController alloc]initWithRootViewController:catVC];
            }
                break;
            case 3:
            {
                
                XSMYViewController * myVC =[[XSMYViewController alloc]init];
                viewController =[[UINavigationController alloc]initWithRootViewController:myVC];
            }
                break;
            default:
                break;
        }
        [_barViewControllers setObject:viewController forKey:@(seletIndex) ];
    }
    
    

    [self.view addSubview:viewController.view];
    
    [self.view bringSubviewToFront:_tabBarView];

    //把上一次的移除掉
    UIViewController * lastVC =[_barViewControllers objectForKey:@(current)];
    [lastVC.view removeFromSuperview];
    
    current =seletIndex;
    
}
-(void)highlitImageView:(NSInteger)index
{
    UIImageView * lastImage =(UIImageView *)[_tabBarView viewWithTag:current+20];
    lastImage.image  =[UIImage imageNamed:norArr[current]];
    
    
    UIImageView * image =(UIImageView *)[_tabBarView viewWithTag:index+20];
    image.image =[UIImage imageNamed:selArr[index]];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
